package com.mstore.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class CalendarUtil {

	/**
	 * 默认日期格式
	 */
	public static String DEFAULT_FORMAT = "yyyy-MM-dd";

	/**
	 * 格式化日期
	 * 
	 * @param date
	 *            日期对象
	 * @return String 日期字符串
	 */
	public static String formatDate(Date date) {
		SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT);
		String sDate = f.format(date);
		return sDate;
	}

	/**
	 * 获取当年的第一天
	 * 
	 * @param year
	 * @return
	 */
	public static Date getCurrYearFirst() {
		Calendar currCal = Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return getYearFirst(currentYear);
	}

	/**
	 * 获取当年的最后一天
	 * 
	 * @param year
	 * @return
	 */
	public static Date getCurrYearLast() {
		Calendar currCal = Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return getYearLast(currentYear);
	}

	/**
	 * 获取某年第一天日期
	 * 
	 * @param year
	 *            年份
	 * @return Date
	 */
	public static Date getYearFirst(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}

	/**
	 * 获取某年最后一天日期
	 * 
	 * @param year
	 *            年份
	 * @return Date
	 */
	public static Date getYearLast(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		Date currYearLast = calendar.getTime();

		return currYearLast;
	}

	/**
	 * 获取当月的第一天
	 * 
	 * @param month
	 * @return
	 */
	public static Date getCurrMonthFirst() {
		Calendar currCal = Calendar.getInstance();
		currCal.set(Calendar.DATE, 1);// 设为当前月的1号
		return currCal.getTime();
	}

	/**
	 * 获取当月的最后一天
	 * 
	 * @param month
	 * @return
	 */
	public static Date getCurrMonthLast() {
		Calendar currCal = Calendar.getInstance();
		currCal.set(Calendar.DATE, 1);// 设为当前月的1号
		currCal.roll(Calendar.DAY_OF_MONTH, -1);
		return currCal.getTime();
	}

	/**
	 * 获取本周的第一天
	 * 
	 * @return
	 */
	public static Date getCurrWeekFirst() {
		Calendar currCal = Calendar.getInstance();
		currCal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return currCal.getTime();
	}

	/**
	 * 获取本周的最后一天
	 * 
	 * @return
	 */
	public static Date getCurrWeekLast() {
		Calendar currCal = Calendar.getInstance();
		currCal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		currCal.add(Calendar.DAY_OF_WEEK, 7);
		return currCal.getTime();
	}
	
	/**
	 * 获取当天日期
	 * * @return
	 */
	public static Date getTodayDate() {
		return new Date();
	}

	/**
	 * 根据月份返回该月起始日期和结束日期
	 */
	public String[] getMonthStartAndEnd(int month, int year) {
		String[] strings = new String[2];
		switch (month) {
			case 1:
				strings[0] = "01-01";
				strings[1] = "01-31 23:59:59";
				return strings;
			case 2:
				strings[0] = "02-01";
				if (isLeapYear(year))
					strings[1] = "02-29 23:59:59";
				else
					strings[1] = "02-28 23:59:59";
				return strings;
			case 3:
				strings[0] = "03-01";
				strings[1] = "03-31 23:59:59";
				return strings;
			case 4:
				strings[0] = "04-01";
				strings[1] = "04-30 23:59:59";
				return strings;
			case 5:
				strings[0] = "05-01";
				strings[1] = "05-31 23:59:59";
				return strings;
			case 6:
				strings[0] = "06-01";
				strings[1] = "06-30 23:59:59";
				return strings;
			case 7:
				strings[0] = "07-01";
				strings[1] = "07-31 23:59:59";
				return strings;
			case 8:
				strings[0] = "08-01";
				strings[1] = "08-31 23:59:59";
				return strings;
			case 9:
				strings[0] = "09-01";
				strings[1] = "09-30 23:59:59";
				return strings;
			case 10:
				strings[0] = "10-01";
				strings[1] = "10-31 23:59:59";
				return strings;
			case 11:
				strings[0] = "11-01";
				strings[1] = "11-30 23:59:59";
				return strings;
			case 12:
				strings[0] = "12-01";
				strings[1] = "12-31 23:59:59";
				return strings;
		}
		return null;
	}

	/**
	 * 判断今天是否是闰年
	 */
	private boolean isLeapYear(int year) {
		GregorianCalendar calendar = new GregorianCalendar(Locale.CHINA);
		return calendar.isLeapYear(year);
	}
	
	/**
	 * 将文本数据解析成日期对象
	 * @throws ParseException 
	 */
	public static Date textParseToDate(String dateStringToParse, String formatter) throws ParseException {
		SimpleDateFormat bartDateFormat = new SimpleDateFormat(formatter);
		Date date = bartDateFormat.parse(dateStringToParse);
		return date;
	}

	public static void main(String[] args) {
		System.out.println(formatDate(getCurrYearFirst()));
		System.out.println(formatDate(getCurrYearLast()));
		System.out.println(formatDate(getCurrMonthFirst()));
		System.out.println(formatDate(getCurrMonthLast()));
		System.out.println(formatDate(getCurrWeekFirst()));
		System.out.println(formatDate(getCurrWeekLast()));
		System.out.println(formatDate(getTodayDate()));
	}
}
